package com.gimi.cloud.jimi.app.consumer.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.jimi.app.consumer.service.RegisterService;
import com.gimi.cloud.jimi.app.consumer.service.UserService;
import com.gimi.cloud.jimi.app.consumer.service.home.AuthenticationStepService;
import com.gimi.cloud.jimi.app.consumer.service.home.HomePageService;
import com.gimi.cloud.jimi.app.consumer.service.moreInfo.MoreInfoService;
import com.gimi.cloud.jimi.app.consumer.service.order.OrderService;
import com.gimi.cloud.jimi.app.consumer.service.product.ProductService;
import com.gimi.cloud.jimi.app.consumer.service.user.UserExpressService;
import com.gimi.cloud.jimi.app.consumer.service.user.UserInfoService;
import com.gimi.cloud.jimi.app.consumer.service.userInfo.*;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.enums.ServiceNameEnum;
import com.gimi.cloud.jimi.app.consumer.redis.IRedisService;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by luck on 2017/09/28.
 */
@RestController
public class JiMiMicroServiceConsumerController {

	private static Logger logger = LoggerFactory.getLogger(JiMiMicroServiceConsumerController.class);
	private final IRedisService iRedisService;
	private final HomePageService homePageService;
	private final RegisterService registerService;
	private final UserService userService;
	private final UserInfoService userInfoService;
	private final UserExpressService userExpressService;
	private final MoreInfoService moreInfoService;
	private final OrderService orderService;
	private final ProductService productService;
	private final WorkInfoService workInfoService;
	private final EmergencyContactService emergencyContactService;
	private final OperatorsService operatorsService;
	private final TaobaoService taobaoService;
	private final AuthenticationStepService authenticationStepService;
	private final FaceInfoService faceInfoService;

	@Autowired

	public JiMiMicroServiceConsumerController(IRedisService iRedisService, HomePageService homePageService, RegisterService registerService,
											  UserService userService,UserInfoService userInfoService,UserExpressService userExpressService,
											  OrderService orderService,ProductService productService,MoreInfoService moreInfoService,
											  WorkInfoService workInfoService, EmergencyContactService emergencyContactService, OperatorsService operatorsService,
											  TaobaoService taobaoService,AuthenticationStepService authenticationStepService, FaceInfoService faceInfoService) {

		this.iRedisService = iRedisService;
		this.homePageService = homePageService;
		this.registerService = registerService;
		this.userService = userService;
		this.userInfoService = userInfoService;
		this.userExpressService=userExpressService;
		this.moreInfoService = moreInfoService;
		this.productService=productService;
		this.orderService = orderService;
		this.workInfoService = workInfoService;
		this.emergencyContactService = emergencyContactService;
		this.operatorsService = operatorsService;
		this.taobaoService = taobaoService;
		this.authenticationStepService = authenticationStepService;
		this.faceInfoService = faceInfoService;
	}

	/**
	 * 作为user服务的统一入口.
	 *
	 * @param requestData
	 *            请求参数
	 * @param request
	 *            httpRequest
	 * @param response
	 *            response
	 */
	@RequestMapping(value = "service")
	public void userService(@RequestBody(required = false) String requestData, @RequestParam(value = "file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("===>进入user微服务主程序入口.");
		logger.info("token===>" + request.getHeader("authorization"));
		logger.info("serviceName===>" + request.getHeader("serviceName"));
		logger.info("userId===>" + request.getHeader("userId"));
		JSONObject jsonObject = JSONObject.parseObject(requestData);
		if(jsonObject == null) {
			jsonObject = new JSONObject();
		}
		jsonObject.put("uid",request.getHeader("userId")==null?request.getHeader("uid"):request.getHeader("userId"));
		jsonObject.put("userId",request.getHeader("userId")==null?request.getHeader("uid"):request.getHeader("userId"));
		jsonObject.put("ip",request.getRemoteAddr());
		requestData = jsonObject.toJSONString();

		Response resp = new Response();

		ServiceNameEnum serviceNameEnum = ServiceNameEnum.getStatus(request.getHeader("serviceName"));
		switch (serviceNameEnum) {
			case APP_HOME_PAGE_COLUMNS:
				homePageService.columnList(requestData,request,response);
				break;
			case APP_HOME_PAGE_ADS:
				homePageService.adList(requestData,request,response);
				break;
			case APP_HOME_COLUMN_PROD:
				homePageService.columnProdList(requestData,request,response);
				break;

			/**app对会员详细信息 userInfo的操作*/
			case USERINFO_ADD:
				userInfoService.addUserInfo(requestData,request,response);
				break;
			case USERINFO_SINGLE:
				userInfoService.getUserInfoSingle(requestData, request, response);
				break;
			case USERINFO_UPDATE:
				userInfoService.updateUserInfo(requestData, request, response);
				break;
				/**app对会员详细配送地址的操作*/
			case USER_EXPRESS_ADD_OR_UPDATE:
				userExpressService.userExpressAddOrUpdate(requestData,request,response);
				break;
			case USEREXPRESS_SINGLE:
				userExpressService.getExpressInfoSingle(requestData, request, response);
				break;
			/*case USEREXPRESS_PAGELIST:
				userExpressService.ExpressPageListByUser(requestData, request, response);
				break;*/

			case USER_GET_REGIST_AGREEMENT:// 获取注册协议
				userService.getRegistAgreement(requestData, response, request);
				break;
            /**注册流程：获取验证码，校验并登陆验证码，设置密码（设置密码后再登陆，就有两种登陆方式：1、验证码；2、密码）*/
			case USER_GET_REGISTERCODE:// 获取注册验证码
				userService.getUserRegisterCode(requestData, response, request);
				break;
			case USER_DOLOGIN_BY_PWDCODE:// 验证码登录
				registerService.doLoginByPwdCode(requestData, response, request);
				break;
			case INIT_PASSWORD://当用户注册完成时，设置密码，可以不设置
				registerService.initPwd(requestData, response, request);
				break;
			case USER_DOLOGIN_BY_PASSWORD:// 用户登录(密码登录)
				registerService.doLoginByPassword(requestData, response, request);
				break;
			/**修改密码有两种，一种是短信验证码修改，一种是原密码修改*/
			case GET_MODIFYPASSWORD_CODE:// 验证码修改密码：获取修改密码验证码
				userService.getModifypasswordCode(requestData, response, request);
				break;
			case USER_CHECK_MODIFYPASSWORD:// 验证码修改密码：校验修改密码验证码
				registerService.checkModifyPassword(requestData, response, request);
				break;
			case MODIFY_PASSWORD_BY_PWDCODE://验证码修改密码：修改密码(通过手机验证码修改)
				registerService.modifyPasswordByPwdCode(requestData, response, request);
				break;
			case MODIFY_PASSWORD://通过原密码修改：修改密码
				registerService.modifyPwd(requestData, response, request);
				break;

			case GETINFORMATION: //发送用户lbs等数据
				userService.getInformation(requestData, response, request);
				break;

			/**订单模块start*/
			case SAVE_ORDER://保存订单
				orderService.addOrder(requestData, response, request);
				break;
			case GET_DATA_STATUS://保存订单前验证数据有效期
				orderService.getDataStatus(requestData, response, request);
				break;
			case GET_ORDER_LIST://获取用户订单列表
				orderService.getOrderList(requestData, response, request);
				break;
			case GET_ORDER_DETAIL://获取订单详情
				orderService.getOrderDetail(requestData, response, request);
				break;
			case GET_REPLANT_DETAIL://租赁订单-租费账期
				orderService.getReplantDetail(requestData, response, request);
				break;
			/**订单模块end*/

			case PEODUCT_DETAIL://商品详情信息展示1:商品基本信息
				productService.showProductDetail(requestData, response, request);
				break;
			case PEODUCT_PROP://商品详情信息展示2:规格参数列表
				productService.getProductProp(requestData, response, request);
				break;
			case SKU_MODEL_VALUE://商品详情信息展示3:有库存的sku规格
				productService.getModelValueList(requestData, response, request);
				break;
			/**********************************************以下为身份认证接口***********************************************/
			case UPLOAD_CARD_PHOTO://上传身份证等图片,其中front为身份证正面，back为身份证背面，avatar为身份证小头像，hand为人脸识别的照片
				userInfoService.uploadCardPhoto(response,request);
				break;
			case UPDATE_USERIDINFO://更新身份证信息
				userInfoService.updateUserIdInfo(requestData,response , request);
				break;
			case GET_USERIDINFO://身份证查询
				userInfoService.getUserInfo(requestData, response, request);
				break;
			case UPDATE_USERBANKCARD://更新银行卡信息
				userInfoService.updateUserbankcard(requestData,response , request);
				break;
			case BANKCARDSIGN: //连连支付签约绑卡前的字符串拼接
				userInfoService.bankCardSign(requestData, response, request);
				break;
			case GETUSERBANKCARD: // 银行卡查询接口
				userInfoService.getUserBankcard(requestData, response, request);
				break;
			/**********************************************************************************************************/


			/**********************************************以下为提速接口***********************************************/
			case GET_ZM_INFO://获取芝麻信用评分
				moreInfoService.getZmInfo(requestData, response, request);
				break;

			case TAO_BAO_LOGIN://淘宝-登录接口
				moreInfoService.taobaoLogin(requestData, response, request);
				break;

			case GET_SALARY_CARD_INFO://获取工资卡信息
				moreInfoService.getSalaryCardInfo(requestData, response, request);
				break;
			case GET_DEBITCARD_LOGIN_PARAMETER://获取蜂巢工资卡基本登录参数
				moreInfoService.getDebitcardLoginParameter(requestData, response, request);
				break;
			case GET_DEBITCARD_LOGIN://蜂巢工资卡登陆
				moreInfoService.getDebitcardLogin(requestData, response, request);
				break;
			case SUB_DEBITCARD_VALIDCODE://蜂巢工资卡登验证码提交接口
				moreInfoService.subDebitcardValidCode(requestData, response, request);
				break;
			case REFRESH_DEBITCARD_VALIDCODE://刷新蜂巢工资卡登验证码接口
				moreInfoService.refreshDebitcardValidcode(requestData, response, request);
				break;


			case GET_SUPPORTED_CREDITCARD_BANKS://获取蜂巢信用卡支持银行列表
				moreInfoService.getSupportedCreditcardBanks(requestData, response, request);
				break;
			case GET_CREDITCARD_LOGIN_PARAMETER://蜂巢信用卡银行登陆接口
				moreInfoService.getCreditcardLoginParameter(requestData, response, request);
				break;
			case SUB_CREDITCARD_BASIC_PARAM://提交蜂巢信用卡基本登录参数
				moreInfoService.subCreditcardBasicParam(requestData, response, request);
				break;
			case GET_CREDITCARD_LOGIN_STATUS://蜂巢信用卡查询登录状态接口
				moreInfoService.getCreditcardLoginStatus(requestData, response, request);
				break;
			case SUB_MISSION_VERIFICATION_CODE://蜂巢信用卡提交验证码接口
				moreInfoService.subMissionVerifiCationCode(requestData, response, request);
				break;
			case SUB_MITDY_NAMICPARAM://蜂巢信用卡刷新验证码接口
				moreInfoService.submitDynamicParam(requestData, response, request);
				break;

			case GET_FENGCHAO_EMAIL_LIST://获取蜂巢信用卡支持的邮箱列表
				moreInfoService.getFengchaoEmailList(requestData, response, request);
				break;
			case GET_FENGCHAO_POSITION_LIST://蜂巢邮箱支持的Position列表
				moreInfoService.getFengchaoPositionList(requestData, response, request);
				break;
			case GET_FENGCHAOE_MAIL_LOGIN://蜂巢邮箱登录
				moreInfoService.getFengchaoEmailLogin(requestData, response, request);
				break;
			case GET_FENGCHAO_EMAIL_STATUS://蜂巢邮箱状态查询接口
				moreInfoService.getFengchaoEmailStatus(requestData, response, request);
				break;
			case SUB_FENGCHAO_EMAIL_INDEPENDENT_PASSWORD://蜂巢邮箱独立密码提交接口
				moreInfoService.subFengchaoEmailIndependentPassWord(requestData, response, request);
				break;
			case SUB_FENGCHAO_EMAIL_VALIDCODE://蜂巢邮箱登验证码提交接口
				moreInfoService.subFengchaoEmailValidCode(requestData, response, request);
				break;

			case GET_PROVIDENTFUND_INFO_SUPPORTED_CITY_LIST://获取公积金城市登录列表
				moreInfoService.getProvidentFundInfoSupportedCityList(requestData, response, request);
				break;
			case GET_PROVIDENTFUND_INFO_CITY_LOGIN_PARAMETER://获取公积金地区登录参数
				moreInfoService.getProvidentFundInfoCityLoginParameter(requestData, response, request);
				break;
			case GET_PROVIDENTFUND_INFO_IMAGE://获取公积金验证码图片
				moreInfoService.getProvidentFundInfoImage(requestData, response, request);
				break;
			case GETP_ROVIDENT_FUND_INFO://登录并且获取公积金信息
				moreInfoService.getProvidentFundInfo(requestData, response, request);
				break;

			case GET_SHEBAO_CITY_LIST://获取社保城市列表
				moreInfoService.getSheBaoCityList(requestData, response, request);
				break;
			case GET_SHEBAO_CITY_LOGIN_PARAMER://获取社保登录参数
				moreInfoService.getSheBaoCityLoginParamer(requestData, response, request);
				break;
			case GET_SHEBAO_CODE_IMAGE://获取社保验证码图片
				moreInfoService.getSheBaoCodeImage(requestData, response, request);
				break;
			case GET_SHEBAO_LOGIN_AND_GRAB://登录并抓取社保账号
				moreInfoService.getSheBaoLoginAndGrab(requestData, response, request);
				break;

			case GET_FACE_INFO://获取面部识别数据
				faceInfoService.getFaceInfo(requestData, response, request);
				break;
			case SAVE_FACE_INFO://保存面部识别数据
				faceInfoService.saveFaceInfo(requestData, response, request);
				break;
			case GET_WORK_INFO://获取工作信息
				workInfoService.getWorkInfo(requestData, response, request);
				break;
			case SAVE_WORK_INFO://"保存工作信息
				workInfoService.saveWorkInfo(requestData, response, request);
				break;
			case GET_EMERGENCY_CONTACT://"获取紧急联系人
				emergencyContactService.findEmergencyContactByUser(requestData, response, request);
				break;
			case SAVE_EMERGENCY_CONTACT://保存紧急联系人
				emergencyContactService.saveEmergencyContact(requestData, response, request);
				break;
			case OPERATOR_APPLICATION://提交申请表单获取回执信息
				operatorsService.application(requestData, response, request);
				break;
			case OPERATOR_GET://采集运行商数据  获取短信验证码
				operatorsService.collect(requestData, response, request);
				break;
			case OPERATOR_SUBMIT://采集运行商数据  提交短信验证码
				operatorsService.submit(requestData, response, request);
				break;
			case TAOBAO_CREATE:
				taobaoService.create(requestData, response, request);
				break;
			case TAOBAO_VALID:
				taobaoService.valid(requestData, response, request);
				break;
				
			//app首页认证步骤接口**	
			case AUTHENTIC_STATUS://获取认证步骤
				authenticationStepService.getAuthenticationStatus(requestData, request, response);
				break;
			case AUTHENTIC_INFO://获取认证详细信息
				authenticationStepService.getAuthenticationInfo(requestData, request, response);
				break;

			/**********************************************************************************************************/
			default:
				resp.setResponseCode(ResponseCodeEnum.SERVICE_NAME_NOT_FOUND);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
		}
		logger.info("===>执行完毕.\n");
	}

}
